namespace PickGold.Charting
{
	using System;
	using System.ComponentModel;
	using System.Security.Permissions;
	using System.Web;

	[AspNetHostingPermission(SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal), AspNetHostingPermission(SecurityAction.InheritanceDemand, Level = AspNetHostingPermissionLevel.Minimal)]
	public abstract class ChartNamedElement : ChartElement
	{
		private string _name;

		protected ChartNamedElement()
		{
			this._name = string.Empty;
		}

		protected ChartNamedElement(string name)
		{
			this._name = string.Empty;
			this._name = name;
		}

		internal ChartNamedElement(IChartElement parent, string name)
			: base(parent)
		{
			this._name = string.Empty;
			this._name = name;
		}

		internal override string ToStringInternal()
		{
			string name = base.GetType().Name;
			if (!string.IsNullOrEmpty(this._name))
			{
				return (name + '-' + this._name);
			}
			return name;
		}

		[DefaultValue("")]
		public virtual string Name
		{
			get
			{
				return this._name;
			}
			set
			{
				if (this._name != value)
				{
					if (this.Parent is INameController)
					{
						INameController parent = this.Parent as INameController;
						if (!parent.IsUniqueName(value))
						{
							throw new ArgumentException(SR.ExceptionNameAlreadyExistsInCollection(value, parent.GetType().Name));
						}
						NameReferenceChangedEventArgs e = new NameReferenceChangedEventArgs(this, this._name, value);
						parent.OnNameReferenceChanging(e);
						this._name = value;
						parent.OnNameReferenceChanged(e);
					}
					else
					{
						this._name = value;
					}
					this.Invalidate();
				}
			}
		}
	}
}

